import Vue from 'vue'
import VueRouter from 'vue-router'

import login from '../pages/login.vue'
import notfound from '../pages/notfound.vue'
import index from '../pages/index.vue'

import goodspage from '../pages/goodspage.vue'
import orderpage from '../pages/orderpage.vue'
import promotionpage from '../pages/promotionpage.vue'
import userpage from '../pages/userpage.vue'
import paypage from '../pages/paypage.vue'




Vue.use(VueRouter);

const router = new VueRouter({
  mode: 'history',
  routes:[
        {
            path: '/',
            redirect: {
                name: 'login'
            }
        },
        {
            path: '/login',
            name: 'login',
            component: login,
            beforeEnter(to, from, next) {
                if (localStorage.gyp_token) {
                    router.replace({
                      name: 'index'
                    });
                } else {
                    next();
                }
            }
        },
        {
            path: '/logout',
            name: 'logout',
            beforeEnter(to, from) {
                delete localStorage.token;
                delete localStorage.user_name;
                delete localStorage.user_type;
                router.push({
                    name: 'login'
                });
            }
        },
        {
            path: '/index',
            redirect: {
                name: 'orderpage'
            },
            name: 'index',
            component: index,
            beforeEnter(to, from, next) {
                if(localStorage.token&&localStorage.token.length>0){
                    next();
                } else {
                    router.push({
                        name: 'login'
                    });
                }
            },
            children: [
                {path:'goodspage', name:'goodspage', component: goodspage},
                {path:'orderpage', name:'orderpage', component: orderpage},
                {path:'promotionpage', name:'promotionpage', component: promotionpage},
                {path:'userpage', name:'userpage', component: userpage},
                {path:'paypage', name:'paypage', component: paypage}
            ]
        },
        {
            path: '*',
            component: notfound
        }
    ]
})

export {router}